<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/7/25
 * Time: 上午10:34
 */

namespace extend;


use think\facade\Cache;

class SMS
{
    const API           = 'http://dc.28inter.com/sms.aspx';
    const ACCOUNT       = 'hlzg';
    const PASSWORD      = 'db2013';
    const USER_ID        = 861;

    /**
     * @purpose 退款成功发送验证码
     * @param string $mobile
     * @param string $message
     * @return bool
     */
    public static function refund (string $mobile, string $message): bool
    {
        if (!preg_match('/^1[3|4|5|6|7|8|9]\d{9}$/', $mobile)) {
            return false;
        }
        // 发送参数
        $data       = [];
        $data['action']     = 'send';
        $data['userid']     = self::USER_ID;
        $data['account']    = self::ACCOUNT;
        $data['password']   = self::PASSWORD;
        $data['mobile']     = $mobile;
        $data['sendTime']   = date('Y-m-d H:i:s', time());
        $data['content']    = '【欢乐中国网】'.$message;
        // 头信息
        $header['Host']     = 'dc.28inter.com';
        $header['Content-Type']     = 'application/x-www-form-urlencoded';
        $header['Connection']       = 'close';
        $result     = \Requests::post(self::API,$header, $data);
        $result     = Functions::xml_to_json($result->body);
        $result     = json_decode($result);
        if ($result->returnstatus === 'Success') {
            // 将验证码存入缓存中
            return true;
        }
        return false;
    }
    /**
     * @purpose 发送短信验证码
     * @param string $mobile
     * @return array
     */
    public static function smsCode (string $mobile): array
    {

        $redis  = Cache::store('redis');
        $expire = $redis->handler()->ttl('expire_'.$mobile);
        if ($expire > 0) {
            return Functions::result(
                STATUS_CODE::FAIL,
                '请在'.$expire.'秒之后重新获取!'
            );
        }
        if (!preg_match('/^1[3|4|5|6|7|8|9]\d{9}$/', $mobile)) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '手机号码格式错误!'
            );
        }
        $code       = Functions::random(4);
        $content    = '【欢乐中国网】您的验证码为: '.$code.'，五分钟内有效';
        // 发送参数
        $data       = [];
        $data['action']     = 'send';
        $data['userid']     = self::USER_ID;
        $data['account']    = self::ACCOUNT;
        $data['password']   = self::PASSWORD;
        $data['mobile']     = $mobile;
        $data['sendTime']   = date('Y-m-d H:i:s', time());
        $data['content']    = $content;
        // 头信息
        $header['Host']     = 'dc.28inter.com';
        $header['Content-Type']     = 'application/x-www-form-urlencoded';
        $header['Connection']       = 'close';
        $result     = \Requests::post(self::API,$header, $data);
        $result     = Functions::xml_to_json($result->body);
        $result     = json_decode($result);
        if ($result->returnstatus === 'Success') {
            // 将验证码存入缓存中
            $redis->set($mobile, $code, 300);
            $redis->set('expire_'.$mobile, $mobile, 60);
            return Functions::result(
                STATUS_CODE::SUCCESS,
                [
                    'code' => md5($mobile.$code)
                ],
                '验证码发送成功!'
            );
        }
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $result->message
        );
    }
}